package com.iwomy.envpro.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.envpro.admin.api.feign.RemoteDeptService;
import com.iwomy.envpro.common.data.datascope.DataScope;
import com.iwomy.envpro.common.security.util.SecurityUtils;
import com.iwomy.envpro.platform.VO.BasicVideoSurveillanceVO;
import com.iwomy.envpro.platform.entity.BasicVideoSurveillanceEntity;
import com.iwomy.envpro.platform.entity.DataTemplateEntity;
import com.iwomy.envpro.platform.mapper.BasicVideoSurveillanceMapper;
import com.iwomy.envpro.platform.service.BasicVideoSurveillanceService;
import com.iwomy.envpro.platform.utils.DeptFillUtils;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.mapper.AirMonitorEquipmentMapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Objects;

/**
 * 视频监控
 *
 * @author pig
 * @date 2023-11-28 14:36:24
 */
@Service
@RequiredArgsConstructor
public class BasicVideoSurveillanceServiceImpl extends ServiceImpl<BasicVideoSurveillanceMapper, BasicVideoSurveillanceEntity> implements BasicVideoSurveillanceService {
	private final AirMonitorEquipmentMapper airMonitorEquipmentMapper;

	private final RemoteDeptService remoteDeptService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveDeep(BasicVideoSurveillanceEntity basicVideoSurveillance) {
		basicVideoSurveillance.setDeptId(SecurityUtils.getUser().getDeptId());
		baseMapper.insert(basicVideoSurveillance);
		if (ObjectUtil.isNull(basicVideoSurveillance.getAirMonitorEquipmentList())) {
			return Boolean.TRUE;
		}
		for (AirMonitorEquipmentEntity airMonitorEquipment : basicVideoSurveillance.getAirMonitorEquipmentList()) {
			airMonitorEquipment.setId(basicVideoSurveillance.getMonitorEquipmentId());
			airMonitorEquipmentMapper.insert(airMonitorEquipment);
		}


		return Boolean.TRUE;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean updateDeep(BasicVideoSurveillanceEntity basicVideoSurveillance) {
		baseMapper.updateById(basicVideoSurveillance);
		for (AirMonitorEquipmentEntity airMonitorEquipment : basicVideoSurveillance.getAirMonitorEquipmentList()) {
			if (Objects.isNull(airMonitorEquipment.getId())) {
				airMonitorEquipment.setId(basicVideoSurveillance.getId());
				airMonitorEquipmentMapper.insert(airMonitorEquipment);
			} else {
				airMonitorEquipmentMapper.updateById(airMonitorEquipment);
			}
		}
		return Boolean.TRUE;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean removeDeep(Long[] ids) {
		baseMapper.deleteBatchIds(CollUtil.toList(ids));
		airMonitorEquipmentMapper.delete(Wrappers.<AirMonitorEquipmentEntity>lambdaQuery().in(AirMonitorEquipmentEntity::getId, ids));
		return Boolean.TRUE;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean removeChild(Long[] ids) {
		airMonitorEquipmentMapper.deleteBatchIds(CollUtil.toList(ids));
		return Boolean.TRUE;
	}

	@Override
	public BasicVideoSurveillanceEntity getOneByVideoDeviceSerial(String deviceSerial) {
		BasicVideoSurveillanceEntity basicVideoSurveillance = new BasicVideoSurveillanceEntity();
		basicVideoSurveillance.setDeviceSerial(deviceSerial);
		LambdaQueryWrapper<BasicVideoSurveillanceEntity> wrapper = Wrappers.lambdaQuery(basicVideoSurveillance);
		return getOne(wrapper);
	}

	@Override
	public Page pageByScope(Page page, BasicVideoSurveillanceEntity basicVideoSurveillance) {

		List data = remoteDeptService
				.listByDeptId(ObjectUtil.isNull(basicVideoSurveillance.getDeptId())?
						SecurityUtils.getUser().getDeptId() :basicVideoSurveillance.getDeptId()).getData();
		basicVideoSurveillance.setDeptId(null);

		LambdaQueryWrapper<BasicVideoSurveillanceEntity> wrapper = Wrappers.lambdaQuery(basicVideoSurveillance);
		wrapper.in(ObjectUtil.isNotEmpty(data), BasicVideoSurveillanceEntity::getDeptId, data);
		Page basicVideoSurveillanceEntityPage = this.getBaseMapper().selectPageByScope(page, wrapper, new DataScope());
		List records = basicVideoSurveillanceEntityPage.getRecords();
		DeptFillUtils.fill(records);
		basicVideoSurveillanceEntityPage.setRecords(records);
		return basicVideoSurveillanceEntityPage;
	}

	@Override
	public List<BasicVideoSurveillanceEntity> listByScope(BasicVideoSurveillanceEntity basicVideoSurveillance) {
		List<BasicVideoSurveillanceEntity> basicVideoSurveillanceEntities = this.getBaseMapper().selectListByScope(Wrappers.lambdaQuery(basicVideoSurveillance), new DataScope());
		return basicVideoSurveillanceEntities;
	}
}